package com.example.demo.controller;

import com.example.demo.controller.vo.ResponseResult;
import com.example.demo.entity.ImageClass;
import com.example.demo.service.ImageClassService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin/image_class")
public class ImageClassController {

    private static final Logger logger = LoggerFactory.getLogger(ImageClassController.class);

    @Autowired
    private ImageClassService imageClassService;

    @PostMapping
    public ResponseResult<ImageClass> createImageClass(
            @RequestHeader("token") String token,
            @RequestParam("name") String name,
            @RequestParam("order") Integer order) {
        logger.info("Creating image class: name={}, order={}", name, order);
        
        ImageClass imageClass = new ImageClass();
        imageClass.setName(name);
        imageClass.setOrder(order);
        
        ImageClass created = imageClassService.createImageClass(imageClass);
        return ResponseResult.success(created);
    }

    @PostMapping("/{id}")
    public ResponseResult<Boolean> updateImageClass(
            @RequestHeader("token") String token,
            @PathVariable("id") Integer id,
            @RequestParam("name") String name,
            @RequestParam("order") Integer order) {
        logger.info("Updating image class: id={}, name={}, order={}", id, name, order);
        
        ImageClass imageClass = new ImageClass();
        imageClass.setId(id);
        imageClass.setName(name);
        imageClass.setOrder(order);
        
        boolean result = imageClassService.updateImageClass(imageClass);
        return ResponseResult.success(result);
    }

    @PostMapping("/{id}/delete")
    public ResponseResult<Boolean> deleteImageClass(
            @RequestHeader("token") String token,
            @PathVariable("id") Integer id) {
        logger.info("Deleting image class: id={}", id);
        
        boolean result = imageClassService.deleteImageClass(id);
        return ResponseResult.success(result);
    }

    @GetMapping("/{page}")
    public ResponseResult<?> getImageClassList(
            @RequestHeader("token") String token,
            @PathVariable("page") Integer page,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit,
            @RequestParam(value = "keyword", required = false) String keyword) {
        logger.debug("Fetching image class list: page={}, limit={}, keyword={}", page, limit, keyword);
        
        Map<String, Object> result = imageClassService.getImageClassList(page, limit, keyword);
        return ResponseResult.success(result);
    }

    @GetMapping("/{id}/image/{page}")
    public ResponseResult<?> getImageListByClass(
            @RequestHeader("token") String token,
            @PathVariable("id") Integer id,
            @PathVariable("page") Integer page,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        logger.debug("Fetching image list by class: id={}, page={}, limit={}", id, page, limit);
        
        Map<String, Object> result = imageClassService.getImageListByClass(id, page, limit);
        return ResponseResult.success(result);
    }
}
